package coreservlets.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class GwtRpc implements EntryPoint {
  private DataServiceAsync serviceProxy;
  private HTML label1;

  @Override
  public void onModuleLoad() {
    serviceProxy = GWT.create(DataService.class);
    Button button1 = new Button("Show Hello World");
    label1 = new HTML("<i>Message will be here</i>");
    button1.addClickHandler(new Button1Handler());
    RootPanel.get("button1").add(button1);
    RootPanel.get("label1").add(label1);
  }

  private class Button1Handler implements ClickHandler {
    @Override
    public void onClick(ClickEvent event) {
      serviceProxy.getButton1Data(new Button1Callback());
    }
  }

  private class Button1Callback implements AsyncCallback<String> {
    @Override
    public void onSuccess(String result) {
      label1.setHTML(result);
    }

    @Override
    public void onFailure(Throwable caught) {
      Window.alert("Unable to get data from server.");
    }
  }
}
